package com.iemes.controller.material;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MaterialAddFormMap;
import com.iemes.entity.MaterialOutFormMap;
import com.iemes.entity.MaterialUpdateFormMap;
import com.iemes.entity.ParameterFormMap;
import com.iemes.mapper.MaterialMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.List;

@Controller
@RequestMapping("/pda/")
public class PDAController extends BaseController {

    @Inject
    private MaterialMapper materialMapper;



    @RequestMapping("materialAddUI")
    public String listUI(){
        return Common.BACKGROUND_PATH + "/material/materialAdd";
    }


    @RequestMapping("materialSubUI")
    public String lisSubtUI() {
        return Common.BACKGROUND_PATH + "/material/materialSub";
    }


    @RequestMapping("materialOutUI")
    public String lisOuttUI(){
        return Common.BACKGROUND_PATH + "/material/materialUpdate";
    }

    @RequestMapping("parameterUI")
    public String parameterUI(){
        return Common.BACKGROUND_PATH + "/material/parameter";
    }

    //获取物料入库信息
    @ResponseBody
    @RequestMapping("getMaterialAdd")
    public PageInfo materialAdd(String pageNow, String pageSize, String column, String sort) throws Exception {
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        String material =  getPara("materialName");
        if(material==null || material.equals("")){
            List<MaterialAddFormMap> pdaAdd = materialMapper.getPdaAdd();
            PageInfo pageInfo = new PageInfo<>(pdaAdd);
            return pageInfo;
        }else {
            List<MaterialAddFormMap> pdaAdd = materialMapper.selPdaAdd(material);
            PageInfo pageInfo = new PageInfo<>(pdaAdd);
            return pageInfo;
        }

    }
    //获取物料出库信息
    @ResponseBody
    @RequestMapping("getMaterialOut")
    public PageInfo materialOut(String pageNow, String pageSize, String column, String sort) throws Exception {
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        //条件查询
        String material =  getPara("materialName");
        if(material==null || material.equals("")){
            List<MaterialOutFormMap> pdaOut = materialMapper.getPdaOut();
            PageInfo pageInfo = new PageInfo<>(pdaOut);
            return pageInfo;
        }else {
            //根据id获取数据
            List<MaterialOutFormMap> pdaOut = materialMapper.selPdaOut(material);
            PageInfo pageInfo = new PageInfo<>(pdaOut);
            return pageInfo;
        }

    }
    //获取物料修改信息

    @ResponseBody
    @RequestMapping("getMaterialUpdate")
    public PageInfo materialUpdate(String pageNow, String pageSize, String column, String sort) throws Exception {
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        List<MaterialUpdateFormMap> pdaUpdate = materialMapper.getPdaUpdate();
        PageInfo pageInfo = new PageInfo<>(pdaUpdate);
        return pageInfo;
    }


    @ResponseBody
    @RequestMapping("getParameter")
    public PageInfo ParameterInfo(String pageNow, String pageSize, String column, String sort) throws Exception {
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }

        String material =  getPara("myValue");
        if(material==null || material.equals("")){
            List<ParameterFormMap> pdaParameter = materialMapper.getPdaParameter();
            PageInfo pageInfo = new PageInfo<>(pdaParameter);
            return pageInfo;
        }else {
            String[] materialSplit = material.split(",");
            List<ParameterFormMap> pdaParameter = materialMapper.selPdaParameter(materialSplit[1],materialSplit[0]);
            PageInfo pageInfo = new PageInfo<>(pdaParameter);
            return pageInfo;
        }
    }

}
